package com.gr.dmbackend.service.Impl.dormaddmin;

import com.alibaba.fastjson.JSONObject;
import com.gr.dmbackend.mapper.EventMapper;
import com.gr.dmbackend.model.Event;
import com.gr.dmbackend.service.dormaddmin.UpdateEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UpdateEventServiceImpl implements UpdateEventService {

    @Autowired
    private EventMapper eventMapper;

    @Override
    public JSONObject updateEvent(Map<String, String> data) {
        JSONObject resp = new JSONObject();

        Integer id = Integer.parseInt(data.get("id"));
        String title = data.get("title");
        String htmlContent = data.get("htmlContent");

        if (title.isEmpty()) {
            resp.put("error_message", "标题不能为空");
            return resp;
        }
        if (htmlContent.isEmpty()) {
            resp.put("error_message", "文章内容不能为空");
            return resp;
        }
        if (title.length() > 40) {
            resp.put("error_message", "标题长度超出限制");
            return resp;
        }

        Event event = eventMapper.selectById(id);
        event.setTitle(title);
        event.setHtmlContent(htmlContent);
        eventMapper.updateById(event);
        resp.put("error_message", "success");

        return resp;
    }
}
